package com.qf.exception;/*
 *保护你的大熊
 *2021/11/17
 *19:29
 */

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常解析器，用来识别异常，处理全局异常
 */
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        ModelAndView modelAndView = new ModelAndView();

        if(e instanceof MyException){
            //判断是自定义的异常
            modelAndView.addObject("error1", "这是自定义异常，不算bug");
            modelAndView.setViewName("jsp/error1");
        }else {
            //判断是系统bug
            modelAndView.addObject("error2", "我这个算bug，跪求原谅");
            modelAndView.setViewName("jsp/error2");
        }

        return modelAndView;
    }
}
